//
//  SHPUserAccount.swift
//  weibo
//
//  Created by songhuipan on 16/4/19.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit

class SHPUserAccount: NSObject,NSCoding {
    /// 获得用户授权后的access_token
    var access_token:String?
    /// access_token过期时间
    var expires_in:Int = 0 {
        // 一获得过期时间，就计算过期时间
        didSet {
            
            expiresDate = NSDate(timeIntervalSinceNow: NSTimeInterval(expires_in))
        }
    }
    /// 用户uid
    var uid:String?
    /// access_token过期时间
    var expiresDate:NSDate?
    /// 用户昵称
    var screen_name:String?
    /// 用户头像地址
    var avatar_large:String?
    
    init(dic: [String :AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dic)
    }
    
    override var description :String {
        
        let keys = ["access_token","expires_in","uid","expiresDate","screen_name","avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        
    }
    // MARK:键值归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    // MARK:解档
    required init?(coder aDecoder: NSCoder) {
        
      access_token = aDecoder.decodeObjectForKey("access_token") as?String
      uid = aDecoder.decodeObjectForKey("uid") as?String
      expiresDate = aDecoder.decodeObjectForKey("expiresDate") as?NSDate
      screen_name = aDecoder.decodeObjectForKey("screen_name") as?String
      avatar_large = aDecoder.decodeObjectForKey("avatar_large") as?String
    }
    
}














